package com.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import com.data.Common;
import com.model.ReferenceModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.data.Common.LineDataSeries;

/**
 * Created by White on 2017/5/12.
 * Email：whitelaning@qq.com
 */

public class AxisYTitle extends View {

    private float xPoint = 0;// 原点X坐标
    private float yPoint = 0;// 原点Y坐标
    private float xLength = 0;// X轴长度
    private float yLength = 0;// Y轴长度

    private Paint mPaint = new Paint();
    private Rect mRect = new Rect();

    private int yStrokeWidth = 1;//画笔宽度
    private ArrayList<String> labels = new ArrayList<>();

    public AxisYTitle(Context context) {
        super(context);
    }

    public void initValue(float Height, float Width) {
        xLength = Width;
        yLength = Height;

        initData();
        initPaint();
    }

    private void initData() {
        for (int i = 1; LineDataSeries != null && i < LineDataSeries.size(); i++) {
            labels.add(Common.LineDataSeries.get(i).name);
        }
    }

    private void initPaint() {
        // 设置画笔
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);// 设置画笔样式
        mPaint.setAntiAlias(true);// 去锯齿
        mPaint.setColor(Common.yScaleColor);// 设置颜色
        mPaint.setTextSize(20);// 设置字体
        mPaint.setStrokeWidth(yStrokeWidth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawYAxisLabel(canvas);
    }

    private void drawYAxisLabel(Canvas canvas) {
        for (int i = 0; i < labels.size(); i++) {
            String title = labels.get(i);

            mPaint.getTextBounds(title, 0, title.length(), mRect);
            int width = mRect.width();//文本的宽度
            int height = mRect.height();//文本的高度

            float y = pointMap.get(i) == null ? 0 : pointMap.get(i);
            if (y > 0) {
                canvas.drawText(title,
                        0,
                        y,
                        mPaint);
            }
        }
    }


    private int findReferenceData(int indexX) {
        for (int i = 0; ; ) {
            if (indexX <= (i + 1) * Common.xScaleEachLength) {
                return i;
            } else {
                i++;
            }

            //做个死循环的保险...
            if (i > 1000) {
                return -1;
            }
        }
    }

    public void refreshData(int indexX) {

        int indexXR = indexX + 2 * Common.xScaleEachLength;

        int referenceIndex = findReferenceData(indexXR);

        ReferenceModel.ListBean item1 = null;
        ReferenceModel.ListBean item2 = null;


        if (referenceIndex != -1) {
            if (referenceIndex == 0) {
                item2 = Common.mReferenceModel.list.get(referenceIndex);
                for (int i = 0; i < item2.height.size(); i++) {
                    float finalY = Common.getYDataPoint(Float.parseFloat(item2.height.get(i)), 0);
                    pointMap.put(i, finalY);
                }
            } else {
                if (referenceIndex + 1 <= Common.mReferenceModel.list.size()) {
                    item2 = Common.mReferenceModel.list.get(referenceIndex);
                    item1 = Common.mReferenceModel.list.get(referenceIndex - 1);

                    /**
                     * y = kx + b
                     */

                    switch (Common.type) {
                        case 0:
                            for (int i = 0; i < item1.height.size()&& i < item2.height.size(); i++) {

                                float y2 = Common.getYDataPoint(Float.parseFloat(item2.height.get(i)), 0);
                                float y1 = Common.getYDataPoint(Float.parseFloat(item1.height.get(i)), 0);

                                float x2 = referenceIndex * Common.xScaleEachLength;
                                float x1 = (referenceIndex - 1) * Common.xScaleEachLength;

                                float k = (y2 - y1) / (x2 - x1);

                                float b = y2 - k * x2;

                                float finalY = k * (indexXR - 2 * Common.xScaleEachLength) + b;

                                if (y1 == y2) {
                                    pointMap.put(i, y2);
                                } else {
                                    pointMap.put(i, finalY);
                                }
                            }
                            break;
                        case 1:
                            for (int i = 0; i < item1.weight.size()&& i < item2.weight.size(); i++) {

                                float y2 = Common.getYDataPoint(Float.parseFloat(item2.weight.get(i)), 0);
                                float y1 = Common.getYDataPoint(Float.parseFloat(item1.weight.get(i)), 0);

                                float x2 = referenceIndex * Common.xScaleEachLength;
                                float x1 = (referenceIndex - 1) * Common.xScaleEachLength;

                                float k = (y2 - y1) / (x2 - x1);

                                float b = y2 - k * x2;

                                float finalY = k * (indexXR - 2 * Common.xScaleEachLength) + b;

                                if (y1 == y2) {
                                    pointMap.put(i, y2);
                                } else {
                                    pointMap.put(i, finalY);
                                }
                            }
                            break;
                        case 2:
                            for (int i = 0; i < item1.head.size() && i < item2.head.size(); i++) {

                                float y2 = Common.getYDataPoint(Float.parseFloat(item2.head.get(i)), 0);
                                float y1 = Common.getYDataPoint(Float.parseFloat(item1.head.get(i)), 0);

                                float x2 = referenceIndex * Common.xScaleEachLength;
                                float x1 = (referenceIndex - 1) * Common.xScaleEachLength;

                                float k = (y2 - y1) / (x2 - x1);

                                float b = y2 - k * x2;

                                float finalY = k * (indexXR - 2 * Common.xScaleEachLength) + b;

                                if (y1 == y2) {
                                    pointMap.put(i, y2);
                                } else {
                                    pointMap.put(i, finalY);
                                }
                            }
                            break;
                    }

                    /*
                    for (int i = 0; i < item1.height.size(); i++) {

                        float y2 = Common.getYDataPoint(Float.parseFloat(item2.height.get(i)), 0);
                        float y1 = Common.getYDataPoint(Float.parseFloat(item1.height.get(i)), 0);

                        float x2 = referenceIndex * Common.xScaleEachLength;
                        float x1 = (referenceIndex - 1) * Common.xScaleEachLength;

                        float k = (y2 - y1) / (x2 - x1);

                        float b = y2 - k * x2;

                        float finalY = k * (indexXR - 2 * Common.xScaleEachLength) + b;


                        Log.e("TAG", "indexXR = " + indexXR);
                        Log.e("TAG", "Common.xScaleEachLength = " + Common.xScaleEachLength);
                        Log.e("TAG", "K = " + k);
                        Log.e("TAG", "b = " + b);


                        Log.e("TAG", "referenceIndex = " + referenceIndex);
                        Log.e("TAG", "item1.height.get(0) = " + item1.height.get(0));
                        Log.e("TAG", "item2.height.get(0) = " + item2.height.get(0));
                        Log.e("TAG", "y1 = " + y1);
                        Log.e("TAG", "y2 = " + y2);
                        Log.e("TAG", "x1 = " + x1);
                        Log.e("TAG", "x2 = " + x2);


                        Log.e("TAG", "FINALY =" + finalY);

                        pointMap.put(i, finalY);
                    }
                    */
                } else if (referenceIndex == Common.mReferenceModel.list.size()) {
                    item2 = Common.mReferenceModel.list.get(referenceIndex - 1);
                    for (int i = 0; i < item2.height.size(); i++) {
                        float finalY = Common.getYDataPoint(Float.parseFloat(item2.height.get(i)), 0);
                        pointMap.put(i, finalY);
                    }
                }
            }
        }

        invalidate();
    }

    public Map<Integer, Float> pointMap = new HashMap<>();
}
